package my.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Goods;
import my.dao.IGoodDao;

public class IGoodsDaoImpl implements IGoodDao {

	private Connection conn = null;

	public IGoodsDaoImpl() {
	}

	public IGoodsDaoImpl(Connection conn) {
		this.conn = conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	@Override
	public int add(Goods good) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;

		try {
			String sql = "insert into goods values(gid.nextval,?,?,?)";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, good.getName());
			prestmt.setDouble(2, good.getPrice());
			prestmt.setInt(3, good.getType());

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("商品插入成功!");
			} else {
				System.out.println("商品插入失败,请重新操作!");
			}
			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public void addGoodList(List<Goods> goods) throws SQLException {
		PreparedStatement prestmt = null;

		try {
			String sql = "insert into goods values(gid.nextval,?,?,?)";
			conn.setAutoCommit(false);
			prestmt = conn.prepareStatement(sql);
			for (Goods good : goods) {
				prestmt.setString(1, good.getName());
				prestmt.setDouble(2, good.getPrice());
				prestmt.setInt(3, good.getType());
				prestmt.addBatch();
			}
			prestmt.executeBatch();
			System.out.println("批量增加成功!");
			conn.commit();
			System.out.println("提交成功!");

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			conn.setAutoCommit(true);
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int deleteByGoodId(int gid) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;
		try {
			String sql = "delete from goods where gid=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, gid);

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败,请重新操作!");
			}
			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int modify(Goods good) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;
		try {
			String sql = "update goods set goods_name=?,goods_price=?,goods_type=? where gid=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, good.getName());
			prestmt.setDouble(2, good.getPrice());
			prestmt.setInt(3, good.getType());
			prestmt.setInt(4, good.getGid());

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("修改成功!");
			} else {
				System.out.println("修改失败,请重新操作!");
			}
			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public List<Goods> findAll() {
		PreparedStatement prestmt = null;
		List<Goods> list = new ArrayList<>();
		ResultSet rs = null;
		try {
			String sql = "select * from goods";
			prestmt = conn.prepareStatement(sql);
			rs = prestmt.executeQuery();
			while (rs.next()) {
				int gid = rs.getInt(1);
				String name = rs.getString(2);
				double price = rs.getDouble(3);
				int type = rs.getInt(4);

				Goods good = new Goods(gid, name, price, type);
				list.add(good);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}

		return list;
	}

	@Override
	public List<Goods> findByType(int type) {
		PreparedStatement prestmt = null;
		List<Goods> list = new ArrayList<>();
		ResultSet rs = null;
		try {
			String sql = "select * from goods where goods_type=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, type);
			rs = prestmt.executeQuery();
			while (rs.next()) {
				int gid = rs.getInt(1);
				String name = rs.getString(2);
				double price = rs.getDouble(3);
				int gtype = rs.getInt(4);

				Goods good = new Goods(gid, name, price, gtype);
				list.add(good);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}

		return list;
	}

	@Override
	public Goods findByGoodId(int gid) {
		PreparedStatement prestmt = null;
		Goods good = null;
		ResultSet rs = null;
		try {
			String sql = "select * from goods where gid=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, gid);
			rs = prestmt.executeQuery();
			if (rs.next()) {
				int gId = rs.getInt(1);
				String name = rs.getString(2);
				double price = rs.getDouble(3);
				int gtype = rs.getInt(4);

				good = new Goods(gId, name, price, gtype);
			}
			return good;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}

		return good;
	}

}
